// components/selectComponent/selectComponent.js
const date = new Date()
const years = []
const months = []
const days = []

for (let i = date.getFullYear(); i <= date.getFullYear()+100; i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}


Component({

  onShareAppMessage() {
    return {
      title: 'picker-view',
      path: 'page/component/pages/picker-view/picker-view'
    }
  },
  /**
   * 组件的属性列表
   */
  properties: {
    nowDate:{
      type:Date
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    years,
    year:date.getFullYear(),
    months,
    month: date.getMonth()+1,
    days,
    date: date.getDate(),
    value: [0, date.getMonth(), date.getDate()-1],
  },


  /**
   * 组件的方法列表
   */
  methods: {
    bindChange(e) {
      const val = e.detail.value
      this.setData({
        year: this.data.years[val[0]],
        month: this.data.months[val[1]],
        date: this.data.days[val[2]],
      })
    },

    // 点击保存或取消，触发父组件修改
    exit(e){
      const {flag} = e.target.dataset
      if(flag == 'save'){
        // 格式化date
        let newDate = `${this.data.year}-${this.data.month}-${this.data.date}`
        // 触发父组件的回调
        this.triggerEvent("tapExit",newDate)
      }else if(flag == 'cancel'){
        this.triggerEvent("tapExit",null)
      }
    }
  },

  // 生命周期
  lifetimes:{
    attached(){
    }
  }
})
